<?php

namespace app\adminapi\controller\v1\system;

use app\adminapi\controller\AuthController;
use app\Request;
use app\services\system\ApplicationServices;
use think\facade\App;

class ApplicationController extends AuthController
{
    /**
     * PostsServices constructor.
     * @param App $app
     * @param PostsServices $service
     * @method temp
     */
    public function __construct(App $app, ApplicationServices $service)
    {
        parent::__construct($app);
        $this->services = $service;
    }

    /**
     * 列表
     * @param Request $request
     * @return mixed
     */
    public function list(Request $request) {
        $where = $request->getMore([
            ['name', '']
        ]);

        return app("json")->success('查询成功',$this->services->pageList($where));
    }


    /**
     * 添加
     * @param Request $request
     * @return mixed
     */
    public function add(Request $request) {
        $data = $request->postMore([
            ['name', ''],
            ['image', ''],
            ['sign', ''],
            ['sort', 0]
        ]);
        $data['add_time'] = time();

        $data['image'] = json_encode($data['image']);
        $data = $this->services->save($data);
        return app("json")->success('添加成功',$data->toArray());
    }

    /**
     * 修改
     * @param Request $request
     * @return mixed
     */
    public function edit(Request $request) {
        $data = $request->postMore([
            ['id', 0],
            ['name', ''],
            ['image', ''],
            ['sign', ''],
            ['sort', 0]
        ]);
        $data['image'] = json_encode($data['image']);
        $data = $this->services->update($data['id'], $data);
        return app("json")->success('修改成功');
    }


    /**
     * 删除
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function del(Request $request, $id) {
        $this->services->delete($id);
        return app("json")->success('删除成功');
    }
}